#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import os
import subprocess
import sys
import xml.etree.ElementTree as ET

project_path = ""


class Constant:
    java_type = "Java"

    react_type = "React"


def checktype():
    if os.path.exists(os.path.join(project_path, "pom.xml")):
        return Constant.java_type
    elif os.path.exists(os.path.join(project_path, "package.json")):
        return Constant.react_type
    else:
        print("Unknown Project:[", Constant.java_type, " ", Constant.react_type, "]")
    pass


# Java Maven 项目检查
class JavaMavenChecker:
    def __init__(self, project_path):
        self.project_path = project_path

    def check_pom_exists(self):
        """检查是否存在pom.xml文件"""
        pom_path = os.path.join(self.project_path, "pom.xml")
        return os.path.exists(pom_path)

    def check_dependency_versions(self):
        """检查是否使用规范版本的依赖"""
        # 示例：确保某些依赖在pom.xml中具有特定版本
        required_dependencies = {
            "spring-boot-starter": "2.7.3",
            "spring-boot-starter-web": "2.7.3",
        }
        with open(os.path.join(self.project_path, "pom.xml"), "r") as pom_file:
            pom_content = pom_file.read()
            for dep, version in required_dependencies.items():
                if f"<artifactId>{dep}</artifactId>" not in pom_content or f"<version>{version}</version>" not in pom_content:
                    return False
        return True

    def check_maven_build(self):
        """检查Maven是否可以成功构建"""
        try:
            result = subprocess.run(["mvn", "clean", "install"], cwd=self.project_path, capture_output=True, text=True)
            return result.returncode == 0
        except FileNotFoundError:
            print("Maven未安装或未配置在系统路径中。")
            return False

    def check(self):
        self.modules()
        # 多模块 单模块
        # jsw layer
        # .dockerignore
        # 插件
        pass

    def modules(self):
        pom_pth = os.path.join(self.project_path, "pom.xml")
        tree = ET.parse(pom_pth)
        root_element = tree.getroot()
        print(root_element.iter("modules"))
        pass


# React.js 项目检查
class ReactChecker:
    def __init__(self, project_path):
        self.project_path = project_path

    def check_package_json_exists(self):
        """检查是否存在package.json文件"""
        package_json_path = os.path.join(self.project_path, "package.json")
        return os.path.exists(package_json_path)

    def check_required_dependencies(self):
        """检查是否包含必要的依赖"""
        required_dependencies = {
            "react": "^18.0.0",
            "react-dom": "^18.0.0",
        }
        with open(os.path.join(self.project_path, "package.json"), "r") as package_file:
            package_content = package_file.read()
            for dep, version in required_dependencies.items():
                if f'"{dep}": "{version}"' not in package_content:
                    return False
        return True

    def check_build_script(self):
        """检查是否配置了构建脚本"""
        with open(os.path.join(self.project_path, "package.json"), "r") as package_file:
            package_content = package_file.read()
            return '"build": ' in package_content

    def check_npm_build(self):
        """检查npm是否可以成功构建"""
        try:
            result = subprocess.run(["npm", "run", "build"], cwd=self.project_path, capture_output=True, text=True)
            return result.returncode == 0
        except FileNotFoundError:
            print("npm未安装或未配置在系统路径中。")
            return False


# 主函数
if __name__ == "__main__":
    project_path = "/Users/mac/mkl/epiphany/"
    project_path = project_path.strip().lower()

    project_type = checktype()
    print(project_type)

    if project_type == Constant.java_type:
        checker = JavaMavenChecker(project_path)
        checker.check()
    elif project_type ==  Constant.react_type:
        checker = ReactChecker(project_path)
        print("检查package.json文件：", "通过" if checker.check_package_json_exists() else "失败")
        print("检查依赖版本：", "通过" if checker.check_required_dependencies() else "失败")
        print("检查构建脚本：", "通过" if checker.check_build_script() else "失败")
        print("检查npm构建：", "通过" if checker.check_npm_build() else "失败")
    else:
        print("不支持的项目类型。")
